//
//  DataFrameMove.m
//  ChampoinPeso
//
//  Created by mac on 2025/2/25.
//

#import "DataFrameMove.h"
#import <AFNetworking/AFNetworking.h>
#import "DataFrameParam.h"

@implementation DataFrameMove

+ (void)startHud {
    //    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    //    [SVProgressHUD setMaximumDismissTimeInterval:2];
    //    [SVProgressHUD show];
//    [MBProgressHUD showMessage:@"" ToView:nil RemainTime:2];
}


+ (void)closedHud {
//    [SVProgressHUD dismiss];
}

+ (void)showErrorHud:(NSString *)hudmsg {
    if ([hudmsg isEqualToString:@"Unable to borrow, already in a loan"]) {
        return;
    }
//    [SVProgressHUD showErrorWithStatus:hudmsg];
    [MBProgressHUD showAutoMessage:hudmsg];
}


+ (void)frameMoverWifi:(void (^)(void))success {
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusReachableViaWWAN:
                success();
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                success();
                break;
            case AFNetworkReachabilityStatusNotReachable:
                break;
            case AFNetworkReachabilityStatusUnknown:
                break;
            default:
                break;
        }
    }];
}


+ (void)frameMoverForPost:(NSString *)moverInfo MoverPost:(BOOL)post frameParm:(NSDictionary *)parm finish:(void (^)(id info))finish {
    [self startHud];
    
    
    NSString *pushUrl = [NSString stringWithFormat:@"%@%@%@",HtpMac,moverInfo,[DataFrameParam frameMoverInfoPart]];;
    NSLog(@"%@",pushUrl);
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    [[session dataTaskWithHTTPMethod:post ? @"POST" : @"GET" URLString:pushUrl parameters:parm headers:nil uploadProgress:^(NSProgress * _Nonnull uploadProgress) {
                
            } downloadProgress:^(NSProgress * _Nonnull downloadProgress) {
                
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                
                NSLog(@"%@:%@",pushUrl,responseObject);
                if([responseObject[@"gardening"] intValue] == 0) {
            
                    [self closedHud];
                    finish(responseObject);
                } else {
                    
                    [self showErrorHud:responseObject[@"hobby"]];
                    if([responseObject[@"gardening"] intValue] == -2){
                        //not login
                        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"stewardess"];
                        [[NSUserDefaults standardUserDefaults] synchronize];
                    }

                }
                
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                [self closedHud];
            }] resume];
}




+ (void)frameMoverImage:(UIImage *)upImage ForPost:(NSString *)moverInfo MoverPost:(BOOL)post frameParm:(NSDictionary *)parm finish:(void (^)(id info))finish {
    [self startHud];
    
    NSString *pushUrl = [NSString stringWithFormat:@"%@%@%@",HtpMac,moverInfo,[DataFrameParam frameMoverInfoPart]];;
    NSLog(@"%@",pushUrl);
    
    AFHTTPSessionManager *imageManager = [AFHTTPSessionManager manager];
    imageManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"image/jpeg",@"image/png",@"application/octet-stream",nil];
    imageManager.requestSerializer= [AFHTTPRequestSerializer serializer];
    imageManager.responseSerializer= [AFHTTPResponseSerializer serializer];
    
    
    [imageManager POST:pushUrl parameters:parm headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
        [formData appendPartWithFileData:[self moreSmall:upImage] name:@"standardWop" fileName:fileName mimeType:@"image/jpeg"];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *res = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingFragmentsAllowed error:nil];
        NSLog(@"%@",res);
        if([res[@"gardening"] intValue] == 0) {
            [self closedHud];
            finish(res);
        } else {
            [self showErrorHud:res[@"hobby"]];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        [SVProgressHUD dismiss];
        [self closedHud];
    }];
}


+ (NSData *)moreSmall:(UIImage *)image {
    NSData *ifno = UIImageJPEGRepresentation(image, 0.1);
    return ifno;
}

+ (void)upData:(NSString *)url paramers:(id)paramers file:(NSData *)file PostsWithBlock:(void (^)(id JSON, NSError *error))block{
    NSString *pushUrl = [NSString stringWithFormat:@"%@%@%@",HtpMac,url,[DataFrameParam frameMoverInfoPart]];;
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"image/jpeg",@"image/png",@"application/octet-stream",nil];
    manager.requestSerializer= [AFHTTPRequestSerializer serializer];
    manager.responseSerializer= [AFJSONResponseSerializer serializer];
    [manager POST:pushUrl parameters:paramers headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [formData appendPartWithFormData:file name:@"fashion"];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@ %@",url,responseObject);
        if (block) {
            block(responseObject,nil);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (block) {
            block(nil,error);
        }
    }];
    
}


@end
